#include "mytextarea.h"
#include <string.h>

#if 0
static void textarea_1(void)
{
    lv_obj_t* ta = lv_textarea_create(lv_scr_act());

    //1.添加文本
    lv_textarea_add_char(ta, 'A');
    lv_textarea_add_text(ta, "BCDkajskjdkjsadkjfksakjgfkjsdfhkllfgdklhklsfdhklsdlkfgkldflsgklfdlgklsfkl");

    //2.设置光标位置
    //0:最左侧； LV_TEXTAREA_CURSOR_LAST：最右侧
    lv_textarea_set_cursor_pos(ta, 0); 

    // 不换行（默认文本过长会换行）
    lv_textarea_set_one_line(ta, true);
}

#endif

lv_obj_t  *ta_username;
lv_obj_t  *ta_password;
lv_obj_t  *label_user;
lv_obj_t  *label_password;
lv_obj_t  *keyboard;

static void event_cb(lv_event_t* e)
{
    lv_event_code_t code = lv_event_get_code(e);
    lv_obj_t* target = lv_event_get_target(e);
    if(target == ta_username)
    {
        if(code == LV_EVENT_FOCUSED) //点击是文本框
        {
            lv_keyboard_set_textarea(keyboard, ta_username);//选中与键盘进行关联
            lv_obj_clear_flag(keyboard, LV_OBJ_FLAG_HIDDEN); //清除隐藏标志，显示键盘
        }
        else if(code == LV_EVENT_VALUE_CHANGED)//点击是键盘
        {
            const char* txt = lv_textarea_get_text(ta_username);
            if(strcmp(txt, "admin") == 0)
                lv_label_set_text(label_user, LV_SYMBOL_OK);
            else
                lv_label_set_text(label_user, "user");
        }
    }
    else if(target == ta_password)
    {
        if(code == LV_EVENT_FOCUSED)
        {
            lv_keyboard_set_textarea(keyboard, ta_password);
            lv_obj_clear_flag(keyboard, LV_OBJ_FLAG_HIDDEN); //清除隐藏标志，显示键盘
        }
        else if(code == LV_EVENT_VALUE_CHANGED)
        {
            const char* txt2 = lv_textarea_get_text(ta_password);
            if(strcmp(txt2, "123456") == 0)
                lv_label_set_text(label_password, LV_SYMBOL_OK);
            else
                lv_label_set_text(label_password, "password");
        }
    }

    // 焦点失去，隐藏键盘
    if(code == LV_EVENT_DEFOCUSED) {
        lv_keyboard_set_textarea(keyboard, NULL);
        lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
    }
}

static void textarea_2(void)
{
    //1.设置用户输入文本框
    ta_username = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(ta_username, 300, 40);
    lv_obj_align(ta_username, LV_ALIGN_TOP_MID, 0, 40);
    lv_obj_remove_style(ta_username, NULL, LV_PART_SCROLLBAR);//移除滚动条
    lv_textarea_set_one_line(ta_username, true);
    lv_textarea_set_placeholder_text(ta_username, "username");
    lv_obj_add_event_cb(ta_username, event_cb, LV_EVENT_ALL, NULL);

    //2.设置密码输入文本框
    ta_password = lv_textarea_create(lv_scr_act());
    lv_obj_set_size(ta_password, 300, 40);
    lv_obj_align_to(ta_password, ta_username, LV_ALIGN_OUT_BOTTOM_MID, 0, 40);//与ta对齐
    lv_obj_remove_style(ta_password, NULL, LV_PART_SCROLLBAR);
    lv_textarea_set_one_line(ta_password, true);
    lv_textarea_set_password_mode(ta_password, true);
    lv_textarea_set_placeholder_text(ta_password, "password");
    lv_obj_add_event_cb(ta_password, event_cb, LV_EVENT_ALL, NULL);

    //3.创建user标签
    label_user = lv_label_create(lv_scr_act());
    lv_label_set_text(label_user, "user");
    lv_obj_set_style_text_font(label_user, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_align_to(label_user, ta_username, LV_ALIGN_OUT_RIGHT_MID, 30, 0);

    //4.创建password标签
    label_password = lv_label_create(lv_scr_act());
    lv_label_set_text(label_password, "password");
    lv_obj_set_style_text_font(label_password, &lv_font_montserrat_20, LV_PART_MAIN);
    lv_obj_align_to(label_password, ta_password, LV_ALIGN_OUT_RIGHT_MID, 30, 0);

    //设置键盘
    keyboard = lv_keyboard_create(lv_scr_act());
    lv_keyboard_set_textarea(keyboard, NULL);
    lv_obj_add_flag(keyboard, LV_OBJ_FLAG_HIDDEN);
}


void mytextarea_example(void)
{
    textarea_2();
}